package cn.itsource.xhy.pet.controller;

import cn.itsource.xhy.org.domain.Employee;
import cn.itsource.xhy.org.domain.Shop;
import cn.itsource.xhy.org.domain.ShopLog;
import cn.itsource.xhy.org.service.IShopService;
import cn.itsource.xhy.pet.domain.Pet;
import cn.itsource.xhy.pet.domain.SearchMasterMsg;
import cn.itsource.xhy.pet.query.SearchMasterMsgQuery;
import cn.itsource.xhy.pet.service.ISearchMasterMsgService;
import cn.itsource.xhy.utils.AjaxResult;
import cn.itsource.xhy.utils.LoginContext;
import cn.itsource.xhy.utils.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {

    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;

    @Autowired
    private IShopService shopService;

    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request){
        System.out.println(searchMasterMsg);
        try {
            searchMasterMsgService.publish(searchMasterMsg,request);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }

    @PostMapping
    public QueryResult<SearchMasterMsg> queryPage(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery,HttpServletRequest request){
        System.out.println("request:"+request);
        Employee employee = (Employee) LoginContext.getObject(request);
        System.out.println("employee:"+employee.getId());

        Shop shop = shopService.loadByAdminId(employee.getId());
        searchMasterMsgQuery.setShopId(shop.getId());
        return searchMasterMsgService.queryPage(searchMasterMsgQuery);
    }

}
